package com.eduadmin.controller;

import com.eduadmin.entity.*;
import com.eduadmin.service.IGradeService;
import com.eduadmin.service.IScoreService;
import com.eduadmin.service.ITeachInfoService;
import com.eduadmin.service.ITeacherService;
import com.eduadmin.utils.ExcelUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class TeachScoreController {
    private final IScoreService scoreService;
    private final ITeachInfoService teachInfoService;
    private final ITeacherService teacherService;
    private final IGradeService gradeService;

    @Autowired
    public TeachScoreController(
            IScoreService scoreService,
            ITeacherService teacherService,
            ITeachInfoService teachInfoService,
            IGradeService gradeService) {
        this.scoreService = scoreService;
        this.teacherService = teacherService;
        this.teachInfoService = teachInfoService;
        this.gradeService = gradeService;
    }

    //hgm 查询教师任教 成绩
    @RequestMapping(value = "queryTeachScore")
    @ResponseBody
    public List<TeachScore> queryTeacherScore(
            @RequestParam(value = "page", required = false) String page,
            @RequestParam(value = "rows", required = false) String rows,
            @RequestParam(value = "gradeName", required = false, defaultValue = "") String gradeName,
            @RequestParam(value = "gradeDatatableName", required = false, defaultValue = "t_score") String gradeDatatableName, //空表 避免首次进入报错
            @RequestParam(value = "teamName", required = false, defaultValue = "") String teamName,
            @RequestParam(value = "semesterName", required = false, defaultValue = "") String semesterName,
            @RequestParam(value = "examName", required = false, defaultValue = "") String examName,
            @RequestParam(value = "teacherName", required = false, defaultValue = "") String teacherName
    ) {
        if(teamName.length()==3){
            teamName =gradeName+teamName;
        }
        Map<String, Object> paraMap = new HashMap<>();           //查询参数map
        List<TeachScore> teachScores = new ArrayList<>();           //返回 查询结果
        List<TeachScore> scores;
        Page pageBean = new Page(Integer.parseInt(page), Integer.parseInt(rows));
        paraMap.put("offset", pageBean.getOffset());
        paraMap.put("rows", pageBean.getRows());


        //根据教师姓名查询教师身份证号
        Teacher teacher = teacherService.getTeacherByTeacherName(teacherName);
        if (teacher == null) {      //没有该教师 直接返回 null
            return teachScores;
        }
        paraMap.clear();
        //查教师的任教信息
        paraMap.put("gradeName", gradeName);
        paraMap.put("teamName", teamName);
        paraMap.put("semesterName", semesterName);
        paraMap.put("teacherIdCard", teacher.getTeacherIdCard());
        List<TeachInfo> teachInfos = teachInfoService.getTeachInfoOfTeacher(paraMap);
        //遍历任教信息
        for (TeachInfo teachInfo : teachInfos) {
            paraMap.clear();
            paraMap.put("gradeName", teachInfo.getGradeName());
            paraMap.put("teamName", teachInfo.getTeamName());
            paraMap.put("semesterName", teachInfo.getSemesterName());
            paraMap.put("courseEnName", teachInfo.getCourseEnName());
            paraMap.put("examName", examName);
            Grade grade = gradeService.getGradeByGradeName(teachInfo.getGradeName()); //得到年级数据库表 从里面查询成绩
            paraMap.put("gradeDatatableName", grade.getGradeDatatableName());

            scores = scoreService.getTeachScores(paraMap);
            for(TeachScore teachScore:scores){  //为成绩增加中文科目名称
                switch (teachInfo.getCourseEnName()){
                    case "chinese":
                        teachScore.setSubjectName("语文");
                        break;
                    case "maths":
                        teachScore.setSubjectName("数学");
                        break;
                    case "english":
                        teachScore.setSubjectName("英语");
                        break;
                    case "physics":
                        teachScore.setSubjectName("物理");
                        break;
                    case "history":
                        teachScore.setSubjectName("历史");
                        break;
                    case "chemistry":
                        teachScore.setSubjectName("化学");
                        break;
                    case "biology":
                        teachScore.setSubjectName("生物");
                        break;
                    case "politics":
                        teachScore.setSubjectName("政治");
                        break;
                    case "geography":
                        teachScore.setSubjectName("地理");
                        break;
                    case "sports":
                        teachScore.setSubjectName("体育");
                        break;
                }
            }
            teachScores.addAll(scores);
        }
        return teachScores;
    }
    //hgm  导出教师任教成绩
    @RequestMapping(value = "adminExportTeachScore")
    public void adminExportTeachScore(
            @RequestParam(value = "gradeName", required = false, defaultValue = "") String gradeName,
            @RequestParam(value = "gradeDatatableName", required = true, defaultValue = "t_score") String gradeDatatableName,
            @RequestParam(value = "teamName", required = false, defaultValue = "") String teamName,
            @RequestParam(value = "semesterName", required = false, defaultValue = "") String semesterName,
            @RequestParam(value = "examName", required = false, defaultValue = "") String examName,
            @RequestParam(value = "teacherName", required = false, defaultValue = "") String teacherName,
            HttpServletResponse response
    ){
        if(teamName.length()==3){
            teamName =gradeName+teamName;
        }
        Map<String, Object> paraMap = new HashMap<>();           //查询参数map
        List<TeachScore> teachScores = new ArrayList<>();           //返回 查询结果
        List<TeachScore> scores;
        //根据教师姓名查询教师身份证号
        Teacher teacher = teacherService.getTeacherByTeacherName(teacherName);
        if (teacher == null) {      //没有该教师 直接返回 null
            return ;
        }
        //查教师的任教信息
        paraMap.put("gradeName", gradeName);
        paraMap.put("teamName", teamName);
        paraMap.put("semesterName", semesterName);
        paraMap.put("teacherIdCard", teacher.getTeacherIdCard());
        List<TeachInfo> teachInfos = teachInfoService.getTeachInfoOfTeacher(paraMap);
        //遍历任教信息
        for (TeachInfo teachInfo : teachInfos) {
            paraMap.clear();
            paraMap.put("gradeName", teachInfo.getGradeName());
            paraMap.put("teamName", teachInfo.getTeamName());
            paraMap.put("semesterName", teachInfo.getSemesterName());
            paraMap.put("courseEnName", teachInfo.getCourseEnName());
            paraMap.put("examName", examName);
            Grade grade = gradeService.getGradeByGradeName(teachInfo.getGradeName()); //得到年级数据库表 从里面查询成绩
            paraMap.put("gradeDatatableName", grade.getGradeDatatableName());
            scores = scoreService.getTeachScores(paraMap);
            for(TeachScore teachScore:scores){  //为成绩增加中文科目名称
                switch (teachInfo.getCourseEnName()){
                    case "chinese":
                        teachScore.setSubjectName("语文");
                        break;
                    case "maths":
                        teachScore.setSubjectName("数学");
                        break;
                    case "english":
                        teachScore.setSubjectName("英语");
                        break;
                    case "physics":
                        teachScore.setSubjectName("物理");
                        break;
                    case "history":
                        teachScore.setSubjectName("历史");
                        break;
                    case "chemistry":
                        teachScore.setSubjectName("化学");
                        break;
                    case "biology":
                        teachScore.setSubjectName("生物");
                        break;
                    case "politics":
                        teachScore.setSubjectName("政治");
                        break;
                    case "geography":
                        teachScore.setSubjectName("地理");
                        break;
                    case "sports":
                        teachScore.setSubjectName("体育");
                        break;
                }
            }
            teachScores.addAll(scores);
        }
        String fileName = "";
        if(teamName.contains(gradeName)){
            fileName=teamName+semesterName+examName+teacherName;
        }else {
            fileName=gradeName+semesterName+examName+teacherName;
        }
        try {
            ExcelUtils.export2Web(response, fileName, "teachscore", TeachScore.class, teachScores);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

}